package hehe.json.json;

import hehe.json.BaseJson;
import hehe.json.util.JsonUtil;

/**
 * Jackson 解析类
 * <B>说明：</B>
 * <pre>
 *  略
 * </pre>
 * <B>示例：</B>
 * <pre>
 *  略
 * </pre>
 * <B>日志：</B>
 * <pre>
 *  略
 * </pre>
 * <B>注意事项：</B>
 * <pre>
 *  略
 * </pre>
 */
public class Jackson extends BaseJson
{
    protected String clazz = "com.fasterxml.jackson.databind.ObjectMapper";

    protected Object json = null;

    public Object getJsonObject()
    {
        if (this.json == null) {
            this.json = JsonUtil.make(this.clazz);
        }

        return this.json;
    }

    @Override
    public String format(Object value)
    {
        try {
            return (String)JsonUtil.invoke(this.getJsonObject(),"writeValueAsString",new Object[]{value},new Class[]{Object.class});
        } catch (Exception ex) {
            return null;
        }
    }

    @Override
    public <T> T parser(String json,Class<T> type)
    {
        try {
            return (T)JsonUtil.invoke(this.getJsonObject(),"readValue",
                    new Object[]{json,type},new Class[]{String.class,Class.class});
        } catch (Exception ex) {
            return null;
        }
    }

}
